// 判断是否json字符串
export function isJsonStr(str: any) {
    if (typeof str == 'string') {
        try {
            let obj = JSON.parse(str);
            // 等于这个条件说明就是JSON字符串 会返回true
            if (typeof obj == 'object' && obj) {
                return true
            } else {
                //不是就返回false
                return false
            }
        } catch (e) {
            return false
        }
    }
    return false
}

/**
 * 计算两个日期之间的天数
 *  date1  开始日期 yyyy-MM-dd
 *  date2  结束日期 yyyy-MM-dd
 *  如果日期相同 返回一天 开始日期大于结束日期，返回0
 */
export function getDaysBetween(date1: string, date2: string): number {
    let startDate = Date.parse(date1);
    let endDate = Date.parse(date2);
    if (startDate >= endDate) {
        return 0;
    }
    let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
    return days;
}

export function replaceCarId(value: string): string {
    if (value) {
        return value.replace(/(.{10}).*(.{4})/, "$1****$2")
    }
    return ''
}

export function replacePhone(value: string): string {
    if (value) {
        return value.replace(/(.{3}).*(.{4})/, "$1****$2")
    }
    return ''
}

export function checkUrl(value: string): boolean {
    const v = new RegExp('^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$', 'i')
    return v.test(value)
}

export function isBetween(num: number, arr: number[]): boolean {
    return num >= Math.min(arr[0], arr[1]) && num <= Math.max(arr[0], arr[1]);
}

// rpx转px
export function rpxToPx(rpx: number): number {
    const screenWidth = uni.getSystemInfoSync().screenWidth
    return (screenWidth * Number.parseInt(rpx + '')) / 750
}

// px转rpx
export function pxToRpx(px: number): number {
    const screenWidth = uni.getSystemInfoSync().screenWidth
    return (750 * Number.parseInt(px + '')) / screenWidth
}